var express = require('express');
var router = express.Router();
var query = require('../db/query')
var trans = require('../db/trans')
var db_group = require('../db/db_group')
var db_user  = require('../db/db_user')
var $ = require('../constant')

router.post('/add',function (req, res, next) {
    let name = req.body.name
    let capacity = req.body.capacity
    let owner = req.body.owner
    trans((conn) => {
        return trans.query(conn, db_group.add.group ,[name, owner, capacity, new Date().getTime()])
            .then(result=>{
                return trans.query(conn, db_group.add.group_id, [])
            }).then(result => {
                return trans.query(conn, db_group.add.group_user, [result[0].GID, owner])
        }).then(result => {
            return  trans.query(conn, db_user.group, [owner])
        })
    }).then(result => {
        res.json({ code: $.CODE_SUCCESS, msg: $.MSG_SUCCESS,data:{ group: result }})
    }).catch(err => {
        console.log(err)
        res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_SYS })
    })
})
module.exports = router